package klk.shop.objects;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import klk.shop.utilities.HTMLUtilities;
import klk.shop.utilities.MysqlUtilities;

public class CustomerVerification extends KLKObject {

	
	protected int customerId;
	protected Date expirationDate;
	protected String hash;
	
	
	public int getCustomerId() {
		return customerId;
	}
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	
	public Date getExpirationDate() {
		return expirationDate;
	}
	public void setExpirationDate(Date expirationDate) {
		this.expirationDate = expirationDate;
	}
	
	public String getHash() {
		return hash;
	}
	public void setHash(String hash) {
		this.hash = hash;
	}
	
	
	public CustomerVerification(int customerId, Date expirationDate, String hash) {
		super();
		this.customerId = customerId;
		this.expirationDate = expirationDate;
		this.hash = hash;
	}
	public CustomerVerification() {
		this(-1, new Date(), "");
	}
	
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("CustomerVerification:" + nl);
		sb.append("customerId:     " + customerId + nl);
		sb.append("expirationDate: " + expirationDate + nl);
		sb.append("hash:           " + hash + nl);
		return sb.toString();
	}
	
	@Override
	public String toHtml() {
		StringBuilder sb = new StringBuilder( "<table>" );
		sb.append( nl );
		sb.append( HTMLUtilities.TableRow("CustomerVerification:") );
		sb.append( HTMLUtilities.TableRow("customerId:", customerId) );
		sb.append( HTMLUtilities.TableRow("expirationDate:", expirationDate) );
		sb.append( HTMLUtilities.TableRow("hash:", hash) );
		sb.append( "</table>" );
		sb.append( nl );
		return sb.toString();
	}
	
	
	public static CustomerVerification getCustomerVerificationFromDatabase(String hash) {
		CustomerVerification cv = new CustomerVerification();
			
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(
				"SELECT " +
					"customer_id, " +
					"expiration_date " +
				"FROM " +
					"customer_verifications " +
				"WHERE hash='"+hash+"';"
			);
			
			rs.next();
			
			cv.setCustomerId(rs.getInt("customer_id"));
			cv.setExpirationDate(rs.getDate("expiration_date"));
			cv.setHash(hash);
			
		} catch (Exception e) {
			e.printStackTrace();
			cv=null;
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return cv;	
	}
	public static int SetCustomerVerificationIntoDatabase(CustomerVerification cv) {
		
		int rowsAffected = 0;

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
				
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			String expirationDateString = MysqlUtilities.toMySqlDateString(cv.getExpirationDate());
			
			String insertString = 
					"INSERT INTO `customer_verifications` (" +
						"customer_id, expiration_date, hash" +
					") VALUES (" +
							  cv.getCustomerId() 		  + ", " +  
						"'" + expirationDateString 	+ "'" + ", " + 	  
						"'" + cv.getHash() 			+ "'" + 
					");";
			rowsAffected = conn.createStatement().executeUpdate( insertString ); 
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (  rs != null) try {   rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return rowsAffected;	
	}
	public static boolean DeleteCustomerVerificationFromDatebase(CustomerVerification cv) {
		boolean success = true;

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;		
		try	{
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();

			String insertString = 
					"DELETE FROM `customer_verifications` " +
					"WHERE " +
						"customer_id=" + cv.getCustomerId() + " AND " +
						"hash='" + cv.getHash() + "'" + 
					";";
			if ( conn.createStatement().executeUpdate( insertString ) < 1 )
				success = false;
			
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
			
		} finally {
			if (  rs != null) try {   rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return success;	
	}

	
	
}
